<template>
  <div class="tab-title-page bill_page">
    <div class="common_form_search">
      <el-form
        ref="searchForm"
        :inline="true"
        :model="searchForm"
      >
        <el-form-item label="订单号">
          <el-input
            style="width:220px"
            v-model="searchForm.orderNo"
            placeholder="请输入订单号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select
            v-model="searchForm.orderStatus"
            placeholder="全部"
            clearable
          >
            <el-option
              v-for="item in selectList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input
            v-model="searchForm.goodsName"
            placeholder="请输入商品名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input
            v-model="searchForm.receivingName"
            placeholder="请输入用户名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="总期数">
          <el-select
            v-model="searchForm.leaseTerm"
            placeholder="全部"
            clearable
          >
            <el-option
              v-for="item in 12"
              :key="item"
              :label="`${item}期`"
              :value="item+''"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下单日">
          <my-date-picker ref="makeOrderPicker"></my-date-picker>
        </el-form-item>
        <el-form-item label="实际支付日">
          <my-date-picker ref="payPicker"></my-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            @click="search()"
          >查询</el-button>
          <el-button
            size="small"
            @click="reset()"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider class="middle_divider"></el-divider>
    <div class="child_page">
      <div class="flex align-center table-top-btns">
        <h5 class="flex align-center">
          <svg-icon
            style="margin-right:15px"
            icon-class="list"
            :size="15"
          />数据列表
        </h5>
        <el-button
          v-if="$hasMethod('#export')"
          size="small"
          icon="el-icon-upload2"
          class="export-btn"
          @click="exportFile"
        >导出</el-button>
      </div>
      <div class="list_wrap">
        <div class="flex list_header">
          <p
            class="text-center"
            style="width:5%"
          >编号</p>
          <p
            class="text-center"
            style="width:5%"
          >总期数</p>
          <p
            class="text-center"
            style="width:9%"
          >租期</p>
          <p
            class="text-center"
            style="width:5%"
          >账单金额</p>
          <p
            class="text-center"
            style="width:6%"
          >实际支付日</p>
          <p
            class="text-center"
            style="width:5%"
          >支付方式</p>
          <p
            class="text-center"
            style="width:5%"
          >实付金额</p>
          <p
            class="text-center"
            style="width:5%;"
          >结算租金</p>
          <p
            class="text-center"
            style="width:6%;"
          >结算时间</p>
          <p
            class="text-center"
            style="width:5%;"
          >结算状态</p>
          <p
            class="text-center"
            style="width:8%;"
          >原因</p>
          <p
            class="text-center"
            style="width:7%;"
          >支付状态</p>
          <p
            class="text-center"
            style="width:7%;"
          >支付流水</p>
          <p
            class="text-center"
            style="width:13%;"
          >用户信息</p>
          <p
            class="text-center"
            style="width:9%;"
          >商品名称</p>
        </div>
        <div
          v-for="item in tableData"
          :key="item.id"
          class="list_item"
        >
          <div class="flex justify-between align-center item_header">
            <el-tag :type="getOrderStatusType(item.status)">{{item.statusName}}</el-tag>
            <p>下单日：{{item.create_time}}</p>
            <p>下单信息：<span
                class="blue-font pointer"
                @click="goToOrder(item.trade_no)"
              >{{item.trade_no}}（{{item.source}}）</span></p>
            <p>实名认证：{{item.cert_name}}-{{item.cert_no}}</p>
          </div>
          <div class="flex item_row one_row">
            <div style="width:5%">{{item.order_id}}</div>
            <div style="width:5%">{{item.month_total}}</div>
            <div style="width:9%">{{item.begin_time}}至{{item.end_time}}</div>
            <div style="width:5%">{{item.rent_total}}</div>
            <div style="width:6%">{{item.pay_time}}</div>
            <div style="width:5%">
              <p v-if="item.status!=0">花呗分期</p>
            </div>
            <div style="width:5%">
              <p v-if="item.status!=0">{{item.payment}}</p>
            </div>
            <div style="width:5%;">
              <p v-if="item.status!=0">{{item.settlement_amount}}</p>
            </div>
            <div style="width:6%;">{{item.settlement_create_time}}</div>
            <div style="width:5%;">{{item.settlement_status}}</div>
            <div style="width:8%;">{{item.settle_refuse_reason}}</div>
            <div style="width:7%;">
              <el-tag :type="item.pay_time?'success':'warning'">{{item.pay_time?'已支付':'未支付'}}</el-tag>
            </div>
            <div style="width:7%;">{{item.alipay_trade_no}}</div>
            <div style="width:13%;">
              <div
                class="full_column"
                style="width: calc(13% - 20px);"
              >
                <el-popover
                  v-if="`${item.prov}-${item.city}-${item.area}${item.address}`.length>30"
                  placement="top-start"
                  width="200"
                  trigger="hover"
                >
                  <p>{{item.realname}} {{item.mobile}}<br />{{item.prov}}-{{item.city}}-{{item.area}}{{item.address}}</p>
                  <p slot="reference">{{item.realname}} {{item.mobile}}<br />{{item.prov}}-{{item.city}}-{{item.area}}{{item.address}}</p>
                </el-popover>
                <p v-else>{{item.realname}} {{item.mobile}}<br />{{item.prov}}-{{item.city}}-{{item.area}}{{item.address}}</p>
              </div>
            </div>
            <div style="width:9%;">
              <div
                class="full_column"
                style="width: calc(9% - 20px);"
              >
                <el-popover
                  v-if="item.goods_title.length>30"
                  placement="top-start"
                  width="200"
                  trigger="hover"
                >
                  <p>{{item.goods_title}}</p>
                  <p slot="reference">{{item.goods_title}}</p>
                </el-popover>
                <p v-else>{{item.goods_title}}</p>
              </div>
            </div>
          </div>
        </div>
        <custom-page
          ref="customPage"
          :total="total"
          @getList="getList"
        ></custom-page>
      </div>
    </div>
  </div>
</template>
<script>
import globalData from "../../utils/global";
import customPage from "@/components/customPage";
import myDatePicker from "@/components/myDatePicker";
import { getOrderStatus, financeHuaBeiList, huaBeiListExport } from "@/api/finance";
export default {
  components: { customPage, myDatePicker },
  data() {
    return {
      searchForm: {
        orderNo: '',
        orderStatus: '',
        goodsName: '',
        receivingName: '',
        leaseTerm: '',
        orderCreateTime: '',
        orderPayTime: ''
      },
      statusList: globalData.orderStatus,
      selectList: [],
      tableData: [],
      total: 0,
    }
  },
  methods: {
    goToOrder(orderId) {
      if (orderId) {
        this.$router.push({ name: 'orderList', params: { orderId } });
      }
    },
    getOrderStatusName(status) {
      const obj = this.statusList.find(item => item.id == status);
      return obj ? obj.name : '';
    },
    getOrderStatusType(status) {
      const obj = this.statusList.find(item => item.id == status);
      return obj ? obj.type : 'warning';
    },
    // 获取数据
    getList(flag) {
      // flag为true, 则不刷新表格
      const { page, pageSize } = this.$refs.customPage;
      if (!flag) {
        this.tableData = [];
      }
      financeHuaBeiList({
        page,
        pageSize,
        ...this.searchForm
      }).then(res => {
        this.tableData = res.data || [];
        this.total = res.total || 0;
      })
    },
    // 查询
    search() {
      const pickerVal1 = this.$refs.makeOrderPicker.getVal();
      const pickerVal3 = this.$refs.payPicker.getVal();
      if (pickerVal1 == 'error' || pickerVal3 == 'error') {
        return;
      }
      this.searchForm.orderCreateTime = pickerVal1;
      this.searchForm.orderPayTime = pickerVal3;
      this.$refs.customPage.page = 1;
      this.getList();
    },
    // 重置
    reset() {
      if (this.$refs.makeOrderPicker) {
        this.$refs.makeOrderPicker.reset();
      }
      if (this.$refs.payPicker) {
        this.$refs.payPicker.reset();
      }
      this.searchForm = {
        orderNo: '',
        orderStatus: '',
        goodsName: '',
        receivingName: '',
        leaseTerm: '',
        orderCreateTime: '',
        orderPayTime: ''
      };
      this.getList();
    },
    getSelect() {
      getOrderStatus().then(res => {
        const list = [];
        for (let key in res) {
          list.push({
            id: key,
            name: res[key]
          })
        }
        this.selectList = list || [];
      })
    },
    exportFile() {
      huaBeiListExport(this.searchForm);
    }
  },
  mounted() {
    this.getSelect();
    this.getList();
  }
}
</script>
<style lang="scss" scoped>
.common_form_search {
  padding: 15px 15px 0 15px;
  background-color: #ffffff;
  border-radius: 0 0 10px 10px;
}
.child_page {
  padding: 15px;
}
.list_wrap {
  position: relative;
}
.list_header {
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  min-height: 50px;
  z-index: 2;
  background: #f5f5f5;
  border: 1px solid #e6e6e6;
  p {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
  }
  p:not(:first-child) {
    border-left: 1px solid #e6e6e6;
  }
}
.list_item {
  position: relative;
  border: 1px solid #e6e6e6;
  margin-top: 10px;
}
.item_header {
  padding: 5px 10px;
  background-color: #f5f5f5;
  & > p {
    color: #666666;
    font-size: 12px;
    padding-left: 20px;
  }
}
.item_row {
  & > div {
    border-top: 1px solid #e6e6e6;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    padding: 5px;
    font-size: 14px;
    line-height: 20px;
    word-break: break-word;
    color: #666666;
    &:not(:first-child) {
      border-left: 1px solid #e6e6e6;
    }
    &.no_border_top {
      border-top: none;
    }
  }
  &.one_row {
    min-height: 130px;
  }
}
.full_column {
  position: absolute;
  top: 0;
  bottom: 10px;
  top: 50px;
  display: flex;
  align-items: center;
  p {
    text-overflow: -o-ellipsis-lastline;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 5;
    line-clamp: 5;
    -webkit-box-orient: vertical;
  }
}
</style>